<?php

namespace app\controller;

use app\BaseController;
use Exception;
use Lib\Crypt;
use think\captcha\facade\Captcha;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Session;
use think\response\Json;

class User extends BaseController
{

    private function checkAuth($uid): bool
    {
        if (empty($uid)) return false;
        try {
            $check = $this->M('user_role')->where('uid', '=', $uid)->where('role_id', '=', 1)->find();
            if (empty($check)) return false; else return true;
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return false;
        }
    }

    /**
     * 用户登陆
     */
    public function login(): Json
    {
        $type = request()->post('type', 0); //1 不使用明文密码登录
        $user = request()->post('user');
        $pass = request()->post('pass');
        $captchaCode = request()->post('captcha_code');
        // $pass_uuid = request()->post('pass_uuid');
        // if (!Captcha::check($captchaCode)) return pr(301, '验证码不正确');
        // if (!hCaptcha($pass_uuid)) return pr(301, '未通过人机验证');
        if (empty($user) || empty($pass)) return json(pr(300, '用户名或密码不可为空')); else {
            $p = Crypt::Encode($pass);
            $f = $this->M("user")->field('password,token,id,nickname,account,head_img,stat')->where(['account' => $user])->find();
            if (empty($f)) {
                $this->M('log_login')->insert(['uid' => 0, 'ip' => getUserIp(), 'time' => time(), 'method' => "网页登录", 'state' => "登录失败[用户不存在]"]);
                return pr(300, '不存在的用户');
            }

            if ($f['password'] != $p) {
                $this->M('log_login')->insert(['uid' => $f['id'], 'ip' => getUserIp(), 'time' => time(), 'method' => "网页登录", 'state' => "登录失败[密码错误]"]);
                return pr(301, '密码错误');
            }

            Session::set('uid', $f['id']);
            $this->M('user')->where(['id' => $f['id']])->save(['last_login_time' => time(), 'last_login_ip' => getUserIp()]);
            if ($f['stat'] == 0) {
                $this->M('log_login')->insert(['uid' => $f['id'], 'ip' => getUserIp(), 'time' => time(), 'method' => "网页登录", 'state' => "登录失败[账号已锁定]"]);
                return pr(301, '账号已锁定');
            }
            $this->M('log_login')->insert(['uid' => $f['id'], 'ip' => getUserIp(), 'time' => time(), 'method' => "网页登录", 'state' => "登录成功"]);
            return pr(200, '登陆成功');

        }
    }

    /**
     * 获取账号token
     */
    public function getToken(): Json
    {
        $uid = $this->getUid();
        if (!$this->checkAuth($uid)) return pr(300, API_NO_AUTH);
        $f = $this->M("user")->field('token')->where(['id' => $uid])->find();
        if (empty($f)) return pr(300, '不存在的用户');
        #if ($f["account_type"]=="1") return pr(300,"该账号已被禁止联机,无法获取Token");

        return pr(200, '获取成功', $f);

    }

    public function role_list(): Json
    {
        try {
            $list = $this->M('role_list')->select()->toArray();
            return pr(200, API_SUCCESS, $list);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return pr(300, API_FAIL, $e->getMessage());
        }
    }

    /**
     * 获取个人信息
     * @return Json
     */
    public function info(): Json
    {
        $token = request()->post('token');
        $uid = $this->getUid();
        if (!empty($token)) $uid = $this->M('user')->where(['token' => $token])->value('id');

        if (empty($uid)) return pr(300, API_NEED_LOGIN);
        $uid2 = request()->post('uid');
        if (!empty($uid2)) $uid = $uid2;
        try {
            
            $f = $this->M("user")->alias('u')->field('u.id,u.stat,u.nickname,u.token,u.account,u.head_img as headurl,u.reg_time,u.last_login_time,signature,background,remark,group_concat(ur.role_id) as role')->leftJoin('user_role ur', 'ur.uid = u.id')->where('u.id', '=', $uid)->group('u.id')->find();

            //获取成就信息
            $achievement = $this->M('achievement_user')->alias('au')->field('a.name,a.icon,a.id,au.time')->join('achievement a', 'a.id = au.aid')->where(['au.uid' => $uid])->select()->toArray();
            $f['_extra'] = ['achievement ' => ["data" => $achievement, 'total' => count($achievement)]];

            //获取未读消息数
            $unreadMessage = $this->M('private_message')->where(['t_uid' => $uid, 'type' => 1])->count();
            $f['_extra']['unreadMessage'] = $unreadMessage;
            return pr(200, API_SUCCESS, $f);
        } catch (DataNotFoundException $e) {
            return pr(301, API_FAIL, $e->getMessage());
        } catch (ModelNotFoundException $e) {
            return pr(302, API_FAIL, $e->getMessage());
        } catch (DbException $e) {
            return pr(303, API_FAIL, $e->getMessage());
        }
    }

    /**
     * 获取主页信息
     */

    public function zone($uid): Json
    {
        try {
            //帖子列表
            $list = $this->M("bbs")->field("title,comment_num as comments,view_num as views,create_time")->where('uid', '=', $uid)->page(1, 10)->select()->toArray();
            //存档列表
            $worldList = $this->M("document")->alias("d")->join("files f", 'f.id = d.file_id')->field("d.file_type,f.name,f.down_times,f.size,f.create_time")->where('d.uid', '=', $uid)->page(1, 10)->select()->toArray();
            //Mod列表
            $modList = $this->M("mod")->field("name,cover_src")->where('uid', '=', $uid)->page(1, 10)->select()->toArray();
            $result = ['bbs' => $list, 'world' => $worldList, 'mod' => $modList];
            return pr(200, API_SUCCESS, $result);
        } catch (DataNotFoundException $e) {
            return pr(300, '不存在的用户');
        } catch (ModelNotFoundException $e) {
            return pr(301, '不存在的模型');
        } catch (DbException $e) {
            return pr(302, '数据库错误', $e->getTraceAsString());
        }
    }

    /**
     * 我的帖子列表
     * @param $uid
     * @return Json
     */
    public function my_bbs_list($uid): Json
    {
        try {
            $sum = 0;
            $page = $this->request->post('page', 1, 'int');
            $limit = $this->request->post('limit', 10, 'int');
            //帖子列表
            $obj = $this->M("bbs")->field("id,cate_id,title,comment_num as comments,view_num as views,create_time")->where('uid', '=', $uid)->where('stat', '=', 1);
            if ($page == 1) $sum = $obj->count();
            $list = $obj->page($page, $limit)->select()->toArray();
            $list['_extra'] = ['sum' => $sum];
            return pr(200, API_SUCCESS, $list);
        } catch (DataNotFoundException $e) {
            return pr(300, '不存在的用户');
        } catch (ModelNotFoundException $e) {
            return pr(301, '不存在的模型');
        } catch (DbException $e) {
            return pr(302, '数据库错误', $e->getTraceAsString());
        }
    }

    /**
     * 我的世界列表
     * @param $uid
     * @return Json
     */
    public function my_world_list($uid): Json
    {
        try {
            $sum = 0;
            $page = $this->request->post('page', 1, 'int');
            $limit = $this->request->post('limit', 10, 'int');
            //存档列表
            $obj = $this->M("document")->alias("d")->join("files f", 'f.id = d.file_id')->field("d.id,d.file_type,f.name,f.down_times,f.size,f.create_time")->where('d.uid', '=', $uid);
            if ($page == 1) $sum = $obj->count();
            $worldList = $obj->page($page, $limit)->select()->toArray();
            $worldList['_extra'] = ['sum' => $sum];
            return pr(200, API_SUCCESS, $worldList);
        } catch (DataNotFoundException $e) {
            return pr(300, '不存在的用户');
        } catch (ModelNotFoundException $e) {
            return pr(301, '不存在的模型');
        } catch (DbException $e) {
            return pr(302, '数据库错误', $e->getTraceAsString());
        }
    }

    /**
     * 我的Mod列表
     * @param $uid
     * @return Json
     */
    public function my_mod_list($uid): Json
    {
        try {
            $sum = 0;
            $page = $this->request->post('page', 1, 'int');
            $limit = $this->request->post('limit', 10, 'int');
            //Mod列表
            $obj = $this->M("mod")->alias('m')->field("m.*,group_concat(mfd.flag_id) as flag_list")->leftJoin("mod_flag_detail mfd", "mfd.mod_id = m.id")->where('m.uid', '=', $uid)->where("m.stat", '>=', 1)->group('m.id');
            if ($page == 1) $sum = $obj->count();
            $modList = $obj->page($page, $limit)->select()->toArray();
            $modList['_extra'] = ['sum' => $sum];
            return pr(200, API_SUCCESS, $modList);
        } catch (DataNotFoundException $e) {
            return pr(300, '不存在的用户');
        } catch (ModelNotFoundException $e) {
            return pr(301, '不存在的模型');
        } catch (DbException $e) {
            return pr(302, '数据库错误', $e->getTraceAsString());
        }
    }

    /**
     * 退出登陆
     */
    public function loginOut(): Json
    {
        $uid = $this->getUid();
        if (empty($uid)) return pr(300, API_NEED_LOGIN);
        Session::set('uid', 0);
        return pr(200, '退出登陆成功');
    }

    /**
     * 用户注册
     */
    public function register(): Json
    {
        return pr(301, '注册通道暂停，请联系社区管理员');
        //$user = request()->post('user');
        $pass = request()->post('pass');
        $nickname = request()->post('nickname');
        $repass = request()->post('repass');
        $email = request()->post('email');
        $captchaCode = request()->post('captcha_code');
        // $pass_uuid = request()->post('pass_uuid');
        if (!Captcha::check($captchaCode)) return pr(301, '验证码不正确');
        // if (!hCaptcha($pass_uuid)) return pr(301, '未通过人机验证');
        if (strlen($pass) > 16) {
            return pr(300, "用户名或密码最长不能超过16位");
        }
        if (strlen($pass) < 6) {
            return pr(300, "用户名或密码长度不能小于6位");
        }
        //        if(!preg_match('/^[a-zA-Z0-9]+$/u',$user)){
        //            return pr(300,"账号只能由字母和数字构成");
        //        }
        if ($pass != $repass) {
            return pr(300, "两次密码不一致，请检查重新填写");
        }

        //限制邮箱只能使用gmail qq outlook
        $desiredSuffixes = ['gmail.com', 'qq.com', 'outlook.com'];
        $domain = strtolower(substr($email, strpos($email, '@') + 1));
        if (!in_array($domain, $desiredSuffixes)) {
            return pr(300, "非特定的邮箱后缀 当前只能使用gmail qq outlook");
        }
        $checkEmail = $this->M('user')->where(['email' => $email])->find();
        if ($checkEmail) {
            return pr(300, "已经有账号绑定此邮箱");
        }


        $data = $this->M('user')->where(['account' => $email])->find();
        if ($data) return pr(300, "账号已存在");

        //邀请码部分
        $invitation = request()->post('invitation');
        if (empty($invitation)) return pr(301, '已启用邀请码注册,请填写邀请码');
        $data = $this->M('invitation_code')->where(['invitation' => $invitation])->find();
        if (empty($data)) return pr(301, '邀请码为空');
        if ($data['avail_num'] - $data['used_num'] <= 0) return pr(301, '此邀请码已被用尽');
        $iid = $data['id'];
        $data = ['valid_time' => time() + 3600, 'account' => $email, 'password' => Crypt::Encode($pass), 'nickname' => $nickname, 'email' => $email];


        $u = getHostUrl('user/verifyRegister?data=' . Crypt::Encode(json_encode($data)));
        SendMail($email, "欢迎加入中文社区", "<h1>欢迎注册SC中文社区，请点击链接进行注册验证，验证通过后即可登陆<a href='$u'>$u</a></h1>");
        return pr(200, "注册验证链接已发送至邮箱，请点击进行验证",$u);
    }

    /**
     * 后台注册
     */
    public function adminRegister(): Json
    {
        $token = request()->post('token');
        if (empty($token)) $uid = $this->getUid(); else
            $uid = $this->M('user')->where(['token' => $token])->value('id');
        if (empty($uid)) return pr(302, API_NEED_LOGIN);
        if (!$this->checkAuth($uid)) return pr(300, API_NO_AUTH);
        
        $remark = request()->post('remark');
        $pass = request()->post('pass');
        $nickname = request()->post('nickname');
        $email = request()->post('email');
        $name = $this->M('user')->where(['nickname' => $nickname])->value('nickname');
        if ($name == $nickname) {
            return pr(300, "该昵称已被使用");
        }
        if (strlen($pass) > 16) {
            return pr(300, "用户名或密码最长不能超过16位");
        }
        if (strlen($pass) < 6) {
            return pr(300, "用户名或密码长度不能小于6位");
        }
        if (empty($remark)) $remark = '';

        $checkEmail = $this->M('user')->where(['email' => $email])->find();
        if ($checkEmail) {
            return pr(300, "已经有账号绑定此邮箱");
        }

        $data = $this->M('user')->where(['account' => $email])->find();
        if ($data) return pr(300, "账号已存在");

        $data = ['valid_time' => time() + 60 * 60 * 24 * 7, 'account' => $email, 'password' => Crypt::Encode($pass), 'nickname' => $nickname, 'email' => $email, 'remark' => $remark];

        $u = getHostUrl('user/verifyRegister?data=' . Crypt::Encode(json_encode($data)));
        SendMail($email, "欢迎加入中文社区", "<h1>欢迎注册SC中文社区，请点击链接进行注册验证，验证通过后即可登陆<a href='$u'>$u</a></h1>");
        return pr(200, "注册验证链接已发送至邮箱，请点击进行验证",$u);
    }


    /**
     * 验证注册
     */
    public function verifyRegister(): string
    {
        $data = request()->get('data');
        if (empty($data)) return '无效参数';
        $d = Crypt::Decode($data);
        $p = json_decode($d, true);
        if (empty($p)) return '无效参数';
        if (time() > $p['valid_time']) return '验证链接已失效，请重新注册!';
        //以下是邀请码部分
        // $iid = $p['iid'];
        // if($iid != 0){
        //     $data = $this->M('invitation_code')->where(['id' => $p['iid']])->find();
        //     if (empty($data)) return '邀请码为空';
        //     if ($data['avail_num'] - $data['used_num'] <= 0) return '此邀请码已被用尽';
        //     unset($p['iid']);
        // }
        unset($p['valid_time']);
        $u = $this->M('user')->field('id')->where(['account' => $p['account']])->find();
        if (!empty($u)) return '验证已成功，无需再次验证';
        $p['reg_time'] = time();
        $p['last_login_ip'] = getUserIp();
        $p['last_login_time'] = time();
        $p['token'] = makeAccessToken();
        $p['money'] = 0;
        $p['account_type'] = 0;
        $id = $this->M('user')->insertGetId($p);
        // if($iid != 0){
        //     $this->M('invitation_used')->insert(['uid' => $id, 'iid' => $iid, 'time' => time()]);
        //     $this->M('invitation_code')->where(['id' => $iid])->setInc('used_num');
        // }
        return '验证成功';
    }

    /**
     * 存档列表
     * @return Json
     */
    public function document_list(): Json
    {
        $type = request()->post('type');
        $uid = $this->getUid();
        try {
            $data = $this->M('document')->alias('d')->field('d.id,f.name,d.file_id')->join('files f', 'f.id = d.file_id')->where('d.uid', '=', $uid)->where('d.file_type', '=', $type)->select()->toArray();
            return pr(200, API_SUCCESS, $data);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return pr(300, API_FAIL);
        }
    }

    /**
     * 存档删除
     * @return Json
     */
    public function document_del(): Json
    {
        $uid = $this->getUid();
        $id = request()->post('id', 0, 'int');
        try {
            $data = $this->M('document')->alias('d')->field('f.id as file_id,f.path')->join('files f', 'f.id = d.file_id')->where('d.id', '=', $id)->where('d.uid', '=', $uid)->find();
            if (!empty($data)) {
                $flag = false;
                try {
                    $flag = unlink($data['path']);
                } catch (Exception $e) {
                }
                $this->M('document')->where('id', '=', $id)->delete();
                $this->M('mod_file')->where('document_id', '=', $id)->delete();
                $this->M('files')->where('id', '=', $data['file_id'])->delete();
                return pr(200, API_SUCCESS, $flag);
            } else return pr(303, API_NO_AUTH);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return pr(300, API_FAIL, $e->getMessage());
        }
    }


    /**
     * 获取用户配置信息
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getConfig(): Json
    {
        $uid = $this->getUid();
        $data = $this->M('user_config')->where(['uid' => $uid])->field("data")->find();
        if (empty($data)) {
            return pr(300, API_FAIL);
        } else {
            return pr(200, API_SUCCESS, $data['data']);
        }
    }

    /**
     * 保存用户配置信息
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function setConfig(): Json
    {
        $data = request()->get('data');
        $uid = $this->getUid();
        $f = $this->M('user_config')->where(['uid' => $uid])->field("data")->find();
        if (empty($f)) {
            $this->M('user_config')->insert(['uid' => $uid, 'data' => $data]);
        } else {
            $this->M('user_config')->where('uid', '=', $uid)->update(['data' => $data]);
        }
        return pr(200, API_SUCCESS, $data['data']);
    }

    /**
     * 获取个人成就信息
     */
    public function getAchievement(): Json
    {
        $uid = $this->getUid();
        try {
            $data = $this->M('achievement_user')->where(['uid' => $uid])->field('uid,aid,time')->select()->toArray();
            if (empty($data)) {
                return pr(300, "成就为空");
            } else {
                return pr(200, API_SUCCESS, $data['data']);
            }
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return pr(300, $e->getMessage());
        }
    }

    /**
     * 修改个人信息
     */
    public function edit(): Json
    {
        $uid = $this->getUid();
        $nickname = request()->post('nickname');
        $avatar = request()->post('avatar');
        $signature = request()->post('signature');
        $background = request()->post('background');
        $data = [];


        if (!empty($nickname)) {
            $myname = $this->M('user')->where(['id' => $uid])->value('nickname');
            $name = $this->M('user')->where(['nickname' => $nickname])->value('nickname');
            if ($name == $nickname && $myname != $nickname) {
                return pr(300, "该昵称已被使用");
            }
            $data['nickname'] = $nickname;
        }
        if (!empty($avatar)) $data['head_img'] = $avatar;
        if (!empty($signature)) $data['signature'] = $signature;
        if (!empty($background)) $data['background'] = $background;

        if (empty($data)) return pr(300, API_FAIL); else {
            $this->M('user')->where(['id' => $uid])->update($data);
            return pr(200, API_SUCCESS);
        }
    }

    /**
     * 修改密码
     */
    public function editPass(): Json
    {
        $uid = $this->getUid();
        $oldPass = request()->post('oldPass');
        $newPass = request()->post('newPass');
        $rePass = request()->post('rePass');
        $captchaCode = request()->post('captcha_code');

        $data = $this->M('user')->where(['id' => $uid])->field('password')->find();
        if (empty($oldPass) || empty($newPass) || empty($rePass)) return pr(300, '参数不全');
        if ($newPass != $rePass) return pr(300, '两次密码不一致');
        // if (!Captcha::check($captchaCode)) return pr(301, '验证码不正确');
        if (Crypt::Encode($oldPass) != $data['password']) return pr(300, '旧密码不正确');

        $this->M('user')->where(['id' => $uid])->update(['password' => Crypt::Encode($newPass), 'token' => makeAccessToken()]);
        return pr(200, API_SUCCESS);
    }

    /**
     * 发送私聊消息
     */
    public function sendPrivateMessage(): Json
    {
        $uid = $this->getUid();
        $toUid = request()->post('toUid');
        $content = request()->post('content');
        if (empty($toUid) || empty($content)) return pr(300, API_FAIL); else {
            $data = ['f_uid' => $uid, 't_uid' => $toUid, 'content' => $content, 'type' => 1, 'create_time' => time()];
            $this->M('private_message')->insert($data);
            return pr(200, API_SUCCESS);
        }
    }

    /**
     * 获取私聊消息列表
     */
    public function getPrivateMessageList(): Json
    {
        $uid = $this->getUid();
        $toUid = request()->post('toUid');
        $page = request()->post('page', 1);
        $limit = request()->post('limit', 10);
        if (empty($toUid)) return pr(300, API_FAIL); else {
            $object = $this->M('private_message')->alias('pm')->field('pm.id,pm.f_uid,pm.t_uid,pm.content,pm.create_time,u.nickname,u.head_img')->leftJoin('user u', 'u.id = pm.f_uid')->where(['pm.f_uid' => $uid, 'pm.t_uid' => $toUid])->whereOr(['pm.f_uid' => $toUid, 'pm.t_uid' => $uid])->order('pm.create_time', 'desc');
            $data = $object->page($page, $limit)->select()->toArray();
            //获取最后一条消息的内容 和 时间
            $lastMessage = $object->order('pm.create_time', 'asc')->find();
            //获取未读消息数量
            $unreadMessage = $object->where(['pm.t_uid' => $uid, 'pm.type' => 1])->count();
            return pr(200, API_SUCCESS, ["data" => $data, "lastMessage" => $lastMessage, "unreadMessage" => $unreadMessage]);
        }
    }

    /**
     * 获取私聊消息内容
     */
    public function getPrivateMessage(): Json
    {
        try {
            $uid = $this->getUid();
            $toUid = request()->post('toUid');
            $page = request()->post('page', 1);
            $limit = request()->post('limit', 10);
            if (empty($toUid)) return pr(300, API_FAIL); else {
                $data = $this->M('private_message')->where(['f_uid' => $uid, 't_uid' => $toUid])->whereOr(['f_uid' => $toUid, 't_uid' => $uid])->order('create_time', 'desc')->page($page, $limit)->select()->toArray();
                return pr(200, API_SUCCESS, $data);
            }
        } catch (Exception $e) {
            return pr(300, API_FAIL);
        }
    }

    /**
     * 撤回私聊消息
     */
    public function revokePrivateMessage(): Json
    {
        $uid = $this->getUid();
        $id = request()->post('id');
        if (empty($id)) return pr(300, API_FAIL);

        $data = $this->M('private_message')->where(['id' => $id, 'from_uid' => $uid])->find();
        if (empty($data)) return pr(300, API_FAIL);
        //大于两分钟禁止撤回
        if (time() - $data['create_time'] > 120) return pr(300, "超过两分钟禁止撤回");
        $this->M('private_message')->where(['id' => $id])->update(['type' => '0']);
        return pr(200, API_SUCCESS);
    }

    /**
     * 我的邀请码列表
     */
    public function my_invitation_code(): Json
    {
        try {
            $uid = $this->getUid();
            if (empty($uid)) return pr(302, API_NEED_LOGIN);
            $data = $this->M('invitation_code')->where(['uid' => $uid])->select();
            return pr(200, API_SUCCESS, $data);
        } catch (Exception $e) {
            return pr(301, '获取列表错误', $e->getMessage());
        }
    }

    /**
     * 生成邀请码
     */
    public function generate_invitation(): Json
    {
        try {
            $token = request()->post('token');
            if (empty($token)) $uid = $this->getUid(); else
                $uid = $this->M('user')->where(['token' => $token])->value('id');
            if (empty($uid)) return pr(302, API_NEED_LOGIN);
            $avail_num = request()->post('avail_num');
            if (empty($avail_num)) return pr(301, '生成邀请码错误', '可用次数不可为空');
            //生成16位随机字符串
            $str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            $invitation = "";
            for ($i = 0; $i < 16; $i++) {
                $invitation .= $str[rand(0, 61)];
            }
            $this->M('invitation_code')->insert(['uid' => $uid, 'create_time' => time(), 'invitation' => $invitation, 'avail_num' => $avail_num, 'used_num' => 0, 'type' => 1, 'void_time' => time() + 3600 * 24 * 7]);
            return pr(200, API_SUCCESS, $invitation);
        } catch (Exception $e) {
            return pr(301, '生成邀请码错误', $e->getMessage());
        }
    }

    /**
     * 作废邀请码
     */
    public function invalidate_invitation(): Json
    {
        try {
            $uid = $this->getUid();
            if (empty($uid)) return pr(302, API_NEED_LOGIN);
            $invitation = request()->post('invitation');
            $this->M('invitation_code')->where(['invitation' => $invitation, 'uid' => $uid])->update(['type' => 0]);
            return pr(200, API_SUCCESS);
        } catch (Exception $e) {
            return pr(301, '生成邀请码错误', $e->getMessage());
        }
    }
}
